<?php

namespace app\command;

use app\model\table\log\OperateLog;
use app\model\tablenot\tool\RedisService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;


/**
 * @title 定时任务
 */
class appLog extends Command
{
    protected function configure()
    {
        $this->setName('appLog')->setDescription("计划任务 appLog");//
    }

    /**
     *   每分钟把所有的日志从缓存写到数据库，允许丢失数据
     */
    protected function execute(Input $input, Output $output)
    {
        ini_set("memory_limit","2560M");
        $RedisServiceModel = new RedisService();
        $redisKey = OperateLog::instance()->getRedisKey();
        $a = $RedisServiceModel->lrange($redisKey, 0, -1);
        $insertPostData = array();
        $maxKey = 0;
        foreach ($a as $key => $val){
            $data = json_decode($val,true);
            $data['content'] = strip_tags($data['content']);
            $insertPostData[] = $data;
            $maxKey = $key;
        }
        $RedisServiceModel->ltrim($redisKey, $maxKey+1, -1);

        try{
            OperateLog::instance()->insertMore($insertPostData);
        }catch (\Exception $e){
           echo $e->getMessage();
        }
        $output->writeln('success');
    }
}
